# -*- coding: utf-8 -*-
from autores.models import Autor, AutorOrdenado
from django.db.models.query_utils import Q
from expertos.models import Experto
from localidades.models import Ciudad, Pais, Lugar
import autocomplete_light

class AutocompleteCiudadPaisItems(autocomplete_light.AutocompleteGenericBase):
    autocomplete_js_attributes = {'placeholder': u'nombre de ciudad/país ..'}
    empty_html_format = u'<span class="div"><em style=\'display:none\'>%s</em><em>No hay resultados</em></span>'
    choices = (
               Pais.objects.all(),
               Ciudad.objects.all(),
    )

    search_fields = (
        ('nombre',),
        ('nombre',),
    )


autocomplete_light.register(AutocompleteCiudadPaisItems)


class AutocompleteAutoresTodosItems(autocomplete_light.AutocompleteGenericBase):
    autocomplete_js_attributes = {'placeholder': u'nombre de autores expertos o no ..'}
    empty_html_format = u'<span class="div"><em style=\'display:none\'>%s</em><em>No hay resultados</em></span>'
    choices = (
               Experto.objects.all(),
               AutorOrdenado.objects.all(),
    )

    search_fields = (
        ('nombre',),
        ('nombre',),
    )


autocomplete_light.register(AutocompleteAutoresTodosItems)

class AutocompleteCiudad(autocomplete_light.AutocompleteModelBase):
    autocomplete_js_attributes={'placeholder': 'Nombre de Ciudad ..'}
    search_fields = ('nombre',)
    empty_html_format = u'<span class="div"><em style=\'display:none\'>%s</em><em>No hay resultados</em></span>'
    def choices_for_request(self):
        q = self.request.GET.get('q', '')
        pais_id = self.request.GET.get('pais_id', None)
        print self.request.GET
        choices = self.choices.all()
        if q:
            choices = choices.filter(nombre__icontains=q)
        if pais_id:
            choices = choices.filter(departamento_provincia__pais__id=pais_id)

        return self.order_choices(choices)[0:self.limit_choices]

autocomplete_light.register(Ciudad,AutocompleteCiudad,add_another_url_name='localidades:ciudad_create',name='AutocompleteCiudad')




class AutocompletePais(autocomplete_light.AutocompleteModelBase):
    autocomplete_js_attributes={'placeholder': 'Nombre de Pais....'}
    search_fields = ('nombre',)
    empty_html_format = u'<span class="div"><em style=\'display:none\'>%s</em><em>No hay resultados</em></span>'
    def choices_for_request(self):
        q = self.request.GET.get('q', '')
        ciudad_id = self.request.GET.get('ciudad_id', None)
        print self.request.GET
        choices = self.choices.all()
        if q:
            choices = choices.filter(nombre__icontains=q)
        if ciudad_id:
            choices = choices.filter(departamentoprovincia__ciudad__id=ciudad_id)

        return self.order_choices(choices)[0:self.limit_choices]

autocomplete_light.register(Pais,AutocompletePais,add_another_url_name='localidades:pais_create',name='AutocompletePais')


#class CiudadPaisItems(autocomplete_light.AutocompleteChoiceListBase):    
#    autocomplete_js_attributes={'placeholder': u'nombre de ciudad/país ..'}
#    choices = Lugar.objects.filter(Q(pais__isnull = False) | Q(ciudad__isnull = False))
#    search_fields = ('nombre',)
#    def choices_for_request(self):
#        q = self.request.GET.get('q', '')
#        if q:
#            self.choices = self.choices.filter(nombre__icontains=q)
#        return self.choices
#
#    
#autocomplete_light.register(CiudadPaisItems)



#class AutocompleteRegion(autocomplete_light.AutocompleteModelBase):
#    autocomplete_js_attributes={'placeholder': 'region name ..'}
#
#    def choices_for_request(self):
#        q = self.request.GET.get('q', '')
#        country_id = self.request.GET.get('country_id', None)
#
#        choices = self.choices.all()
#        if q:
#            choices = choices.filter(name_ascii__icontains=q)
#        if country_id:
#            choices = choices.filter(country_id=country_id)
#
#        return self.order_choices(choices)[0:self.limit_choices]